package day0727API;

import java.util.Vector;

public class Test8 {
    public static void main(String[] args) {
        BunShop shop=new  BunShop();
        Thread boss=new Thread(){
            public void run() {
                while(true){
                    shop.makeBun();
                }
            }
        };
        boss.start();


        Thread consumer=new Thread(()-> {
            while(true) {
                shop.sellBun();
            }
        });
        consumer.start();
    }
}


class BunShop{//面包店
    Vector<String>  buns=new Vector<>();
    public synchronized  int makeBun(){
        try {
            if(buns.size()>=10){
                System.out.println("老板已经做好了10个面包,准备休息");
                this.wait();//当前线程进入阻塞
            }
            System.out.println("老板开始做面包,目前是第："+(buns.size())+"个");
            buns.add("面包");
            System.out.println(buns);
            Thread.sleep(800);
            System.out.println();
            this.notifyAll();
        }catch (InterruptedException e){
            e.printStackTrace();
        }
        return buns.size();
    }




    public synchronized int sellBun(){
        try {
            if(buns.size()<=0){
                System.out.println("消费者已经买完所有的面包了，消费者线程需要等待");
                this.wait();//当前线程进入阻塞
            }
            System.out.println("消费者开始买面包，买第："+(buns.size()));
            buns.remove(0);
            Thread.sleep(800);
            System.out.println();
            this.notifyAll();
        }catch (InterruptedException e){
            e.printStackTrace();
        }
        return buns.size();
    }
}